package com.wondersgroup.insurance.dnuilearn.viewgroups.viewpager;

import android.animation.ValueAnimator;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/6/21.
 */

public class WelcompagerTransformer1 implements ViewPager.PageTransformer {

    /*
    * 平行空间welcome
    *  private int[] layouts = {
            R.layout.welcome4,
            R.layout.welcome5,
            R.layout.welcome4
    };
    *
    * */

    final int degree= 30;

    @Override
    public void transformPage(View view, float position) {
        Log.e("","position  = "+position);
        ImageView image_bg1 = (ImageView) view.findViewById(R.id.image_bg1);
        ImageView image_phone_content1 = (ImageView) view.findViewById(R.id.image_phone_content1);
        ImageView image_phone_content2 = (ImageView) view.findViewById(R.id.image_phone_content2);

        if (-1 < position && 1 > position){


            image_bg1.setPivotX(0);
            image_bg1.setPivotY(image_bg1.getHeight());
            if (position < 0){
                image_bg1.setRotation(degree * position);
            }else if (position>0){
                image_bg1.setRotation((90-degree) * position);
            }else {
                translate(image_phone_content1,image_phone_content2);
            }

        }else {
            image_phone_content1.setTranslationX(0);
            image_phone_content2.setTranslationX(image_phone_content1.getWidth());
        }
    }


    void translate(final  View v1, final View v2){

        if (v1.getTranslationX() == 0){
            ValueAnimator animator = ValueAnimator.ofFloat(0,-1);

            animator.setDuration(200);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();

                    float translatex = v1.getWidth()*value;
                    v1.setTranslationX(translatex);
                    v2.setTranslationX(translatex);
                }
            });
            animator.start();
        }

    }

}
